#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


#
#	Scratch workspace.
#

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from Tweet_model import GeoTweet_model
from Tweet_model import GML

import operator
import time
import datetime
import header
import footer
import urllib2
import simplejson
import re
import Stalker
from urllib2 import *
import httplib, urllib
#Thanks To Mike Dirolf
#http://dirolf.com/project/pyporter2
import Stemmer

class Cleaner(webapp.RequestHandler):
	def get(self):
		query = GeoTweet_model.all()
		query = db.Query(GeoTweet_model)
			
		#How old the tweets are
		#query.filter("geo =",None)
		
		query.filter('geo !=',None)
		
		
			#Query only once, then do fancy things
			#Mental note: When deploy, put in LIMIT 20
		q_res = query.fetch(limit = 10)
		#now = datetime.now()
		#now = datetime.datetime(2011,11,28,16,4,24)
		now = datetime.datetime.now()
		#time_limit = now - datetime.timedelta(hours = 2)
		
		self.response.out.write(now)
		#self.response.out.write("<br>"+str(time_limit)+"<br>")
		for tw in q_res:
			self.response.out.write(tw.id_str+" "+str(tw.geo)+str(tw.real_name)+str(tw.created_at)+"<br>")
		
		for tw in q_res:
			
			#if tw.id_str == str("\"270010789\""):
			#	self.response.out.write("hello")
				#db.delete(tw)
			#self.response.out.write(now - datetime.timedelta(minutes = 3))
			self.response.out.write("<br><br>")
			if (now - datetime.timedelta(hours = 4) > tw.created_at):
			#if (now - time_limit) > (tw.created_at - time_limit):
				self.response.out.write("<br>DELETE THIS: ")
				self.response.out.write(tw.id_str+" "+str(tw.geo)+str(tw.real_name)+str(tw.created_at)+"<br>")
				db.delete(tw)
			else:
				self.response.out.write("<br>Still valid: ")
				self.response.out.write(tw.id_str+" "+str(tw.geo)+str(tw.real_name)+str(tw.created_at)+"<br>")